package com.hzz.web.app.user;

import com.hzz.api.user.IOrgInfoService;
import com.hzz.dto.user.OrgInfoDto;
import com.hzz.framework.PageUtil;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.ALogOperation;
import com.hzz.framework.annotation.RfUser2Bean;
import com.hzz.framework.annotation.auth.Logical;
import com.hzz.framework.annotation.auth.RequiresPermissions;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.web.controller.BaseController;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "组织架构")
@RestController("apiOrgInfoController")
@Slf4j
public class OrgInfoController extends BaseController {
    @Autowired
    IOrgInfoService orgInfoService;
    static final String acPrefix = "/api/staff/orgInfo/";

    //@RequiresPermissions("orgInfo:menu")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "page/{pageNum}")
    // @ApiOperation(value = "信息分页(未删除)")
    public Response page(@RequestBody OrgInfoDto dto, @PathVariable("pageNum") Integer pageNum) {
        log.info("page.........");
        Response result = new Response();
        try {
            dto.setPageSize(CommonConstant.PAGEROW_DEFAULT_COUNT);
            dto.setPageNum(pageNum);
            dto.setDelFlag(0);
            result.data = PageUtil.copy(orgInfoService.findDataIsPage(dto));
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    /**
     * <p> 信息树json。
     */
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "tree")
    @ResponseBody
    public Response tree() {
        log.info("tree.........");
        Response result = new Response(0, "success");
        try {
            result.data = orgInfoService.findDataIsTree(null);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    //@RequiresPermissions("orgInfo:menu")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "list")
    // @ApiOperation(value = "信息列表(未删除)")
    public Response list(@RequestBody OrgInfoDto dto) {
        log.info("list.........");
        Response result = new Response();
        try {
            dto.setDelFlag(0);
            result.data = orgInfoService.findDataIsList(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }


    //@RequiresPermissions("orgInfo:info")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "info/{id}")
    // @ApiOperation(value = "信息详情")
    public Response info(@PathVariable("id") Long id) {
        log.info("info.........");
        Response result = new Response();
        try {
            OrgInfoDto dto = new OrgInfoDto();
            dto.setId(id);
            dto.setDelFlag(0);
            result.data = orgInfoService.findDataById(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }
}